//
//  DataCollectorAppDelegate.m
//  DataCollector
//
//  Created by jiro on 12/06/09.
//  Copyright 2012 jiro music. All rights reserved.
//

#import "DataCollectorAppDelegate.h"

#import "DbInisializer.h"
#import "DataCollector/Yahoo/YahooHtmls.h"
#import "../Strategies/Strategies.h"
#import "../DbTypes/DbTypes.h"

@implementation DataCollectorAppDelegate

@synthesize window;
@synthesize outputTextField;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
//	NSLog( @"%@", [ CurrencyExchangeType queryCreateTable ] );
//	NSLog( @"%@", [ CurrencyExchangeType queryInsertInitialValues ] );
}

- (IBAction) createAndInitilizeTables: (id) sender
{
	//	create database
	NSArray* arguments = [ NSArray arrayWithObject: @"MarketDatabase" ];
	NSTask* task = [ NSTask launchedTaskWithLaunchPath: @"/usr/local/pgsql/bin/createdb" arguments: arguments ];
	while ( YES )
	{
		if ( [ task isRunning ] )
		{ 
			[ NSThread sleepForTimeInterval: 1 ];
		}
		else
		{
			break;
		}
	}
	NSString* s = @"";
	[ DbInisializer intializeDb: s ];
	if ( ! [ s isEqualToString: @"" ] )
	{
		[ outputTextField setStringValue: s ];
	}
}

- (IBAction) collectCurrencyExchangeData: ( id ) sender;
{
	YahooCurrencyExchangeHtmlOperator* ope =
		[ [ YahooCurrencyExchangeHtmlOperator alloc ] initWithFileSaveFolder: @"/Temporary" ];
	[ ope save ];

	NSString* errorMessage = [ ope errorMessage ];
	if ( errorMessage != nil )
	{
		NSLog( @"%@", errorMessage );
	}

}

- (IBAction) dropDb: (id) sender
{
	NSArray* arguments = [ NSArray arrayWithObject: @"MarketDatabase" ];
	NSTask* task = [ NSTask launchedTaskWithLaunchPath: @"/usr/local/pgsql/bin/dropdb" arguments: arguments ];
	while ( YES )
	{
		if ( [ task isRunning ] )
		{ 
			[ NSThread sleepForTimeInterval: 1 ];
		}
		else
		{
			break;
		}
	}
}

- (IBAction) createMaTableData: (id) sender
{
	int lengths[] = { 5, 15, 25, 50, 75 };
	for ( int i = 0, count = sizeof( lengths ) / sizeof( int ); i < count; i++ )
	{
		[ DbInisializer createCurrencyMaValuesWithAverageLength: lengths[ i ] ];
		[ DbInisializer createCurrencyLiquidityMaData: lengths[ i ] ];
	}
}

- (IBAction) createCurrencyLiquidityData: (id) sender
{
	[ DbInisializer createCurrencyLiquidityData ];
}

@end
